package com.ynny.dao;

import com.ynny.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("userManage")
public class UserManageImpl implements UserManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public boolean login(User user) {
        //定义SQL查询语句，实现根据用户信息查询users表中的用户记录
        String sql = "select * from users where number=? and password=? and identity=?";
        //定义数组来存放SQL语句中的参数
        Object[] obj = new Object[]{
                user.getNumber(),
                user.getPassword(),
                user.getIdentity()
        };

        //创建一个BeanPropertyRowMapper对象，用来映射查询结果到User对象中
        RowMapper<User> rowMapper;

        BeanPropertyRowMapper<User> rowMapper1 = new BeanPropertyRowMapper<>(User.class);

        //执行SQL查询语句，通过rowMapper返回存放User类的对象的List集合
        List<User> user_db=jdbcTemplate.query(sql,rowMapper1,obj);
        //判断用户记录不为空

        if (!user_db.isEmpty()){
            return true;
        }
        return false;
    }

    @Override
    public void quit() {

        System.out.println("退出登录");
    }
}
